package com.example.backend.controller;

import com.example.backend.common.Result;
import com.example.backend.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserService userService;
    @GetMapping("/getUserList")
    public Result getUserList(){
        System.out.println("getUserList");
        Result result=userService.getUserList();
        System.out.println(result.getMsg());
        return result;
    }
    @GetMapping("/selectUser")
    public Result selectByUid(@RequestParam String uid){
        System.out.println("selectUser");
        System.out.println(uid);
        Result result=Result.success("success").data(uid,userService.selectByUid(uid));
        System.out.println(result.getData().get(uid));
        return result;
    }
    @GetMapping("/login")
    public Result login(@RequestParam String uid,@RequestParam String password){
        System.out.println("login");
        Result result=userService.login(uid,password);
        return result;
    }
    @GetMapping("/changeRole")
    public Result changeRole(@RequestParam String uid){
        System.out.println("changeRole");
        Result result=userService.changeRole(uid);
        return result;
    }
    @GetMapping("/addUser")
    public Result addUser(@RequestParam String password,@RequestParam int role){
        System.out.println("addUSer");
        System.out.println(role);
        if(role==1){
            return userService.addUser(password,"admin");
        }else if(role==2){
            return userService.addUser(password,"user");
        }
        return Result.error("参数错误");
    }
}
